$(".appIntr>input")[0].addEventListener("input", function() { //字数监听
    var text = $(this).val();
    var length = text.length;
    $(".appIntr>span>i").html(length);
})
$(".limitnum-direct>textarea")[0].addEventListener("input", function() { //字数监听
    var text = $(this).val();
    var length = text.length;
    $(".limitnum-direct>span>i").html(length);
})
new Upload({ //logo上传
    elem: '.add-file-btn',
    url: '/Api/Upload/ajax_upload_file',
    name: 'Filedata',
    formId: 'upload-form',
    inputId: 'file-upload-input',
    success: function(json) {
        if (json.code == 1) {
            this.btn.parent().find(".list").remove();
            var txt = '<div class="list">' +
                '<img src="' + json.url + '" alt="">' +
                '<div class="operate">' +
                '<i class="iconfont preview-img" style="left:33px;">&#xe615;</i>' +
                '</div>' +
                '<div class="musk"></div>' +
                '<input type="hidden" name="logo" value="' + json.url + '">' +
                '</div>';
            this.btn.before(txt);
            headurl = json.url;
            $.alert('上传成功');
            this.btn.text("修改");
            this.btn.css({ fontSize: "15px" });
        }
    },
    error: function() {
        $.alert('上传失败');
    }
});
//添加成员表单验证
var mainForm = $('#addmember-form');
mainForm.validate();
mainForm.on('submit', function() {
        if (mainForm.valid()) {
            if ($('.addmemberurl>.list>img').length <= 0) {
                $.alert('请上传照片!');
                return false;
            } else {
                var name = $(".memberName").val()
                var url = $(".addmemberurl>.list>img").attr("src");
                var position = $(".memberPosition").val()
                $(".memberlist").append("<tr><td><input type='hidden' name='logo_img[]' value='" + url + "' />   <img src='" + url + "' alt='' style='width:100px;height:100px' /></td><td><input type='hidden' name='member_name[]' value='" + name + "' />  <span>" + name + "</span></td><td><span>" + position + "</span><input type='hidden' name='member_job[]' value='" + position + "' /> </td><td class='mr'><a class='iconright editmember blue-color' href='javascript:;'data-target='#editimgModal' data-toggle='modal' ><i class='iconfont'>&#xe605;</i>编辑</a><a class='iconright blue-color delete' href='javascript:;' style='color:#f58381;''><i class='iconfont' >&#xe65f;</i>删除</a></td></tr>")
                $("#imgModal").addClass("hide")
            }
            return false;
        }
    })
    //编辑成员
var tds = {}
$("body").on("click", ".editmember", function(e) {
    var e = event.target;
    if (e.tagName == "I") {
        tds = $(e).parent("a").parent("td").siblings();
    } else {
        tds = $(e).parent("td").siblings();
    }
    var imgurl = $(tds[0]).find("img").attr("src")
    var editname = $(tds[1]).text()
    var editposition = $(tds[2]).text()
    $(".editmemberName").val(editname)
    $(".editmemberPosition").val(editposition)
    $(".editmemberurl>.list>img").attr("src", imgurl)
})
$(".editconfirmbtn").click(function() {
        var editmemberForm = $('#editmember-form');
        editmemberForm.validate();
        if (editmemberForm.valid()) {
            var name = $(".editmemberName").val()
            var position = $(".editmemberPosition").val()
            var url = $(".editmemberurl>.list>img").attr("src")
            $(tds[1]).find("span").text(name)
            $(tds[1]).find("input").attr("value", name)
            $(tds[2]).find("span").text(position)
            $(tds[2]).find("input").attr("value", position)
            $(tds[0]).find("img").attr("src", url)
            $(tds[0]).find("input").attr("value", url)
            $("#editimgModal").addClass("hide")

        }
        return false;
    })
    //添加投资机构验证
var addmechanismForm = $('#addmechanism-form');
addmechanismForm.validate();
addmechanismForm.on('submit', function() {
        if (addmechanismForm.valid()) {
            if ($('.mechanismimg>.list>img').length <= 0) {
                $.alert('请上传照片!');
                return false;
            } else {
                var name = $(".mechanismName").val()
                var url = $(".mechanismimg>.list>img").attr("src");
                $(".mechanismlist").append("<tr><td><input type='hidden' value='" + url + "' name='agency_logo[]' /><img src='" + url + "' alt='' style='width:100px;height:100px' /></td><td><input type='hidden' value='" + name + "' name='agency_name[]' /><span>" + name + "</span> </td><td class='mr'><a class='iconright editmechanism blue-color' href='javascript:;' data-target='#editmechanismModal' data-toggle='modal' ><i class='iconfont'>&#xe605;</i>编辑</a><a class='iconright blue-color delete' href='javascript:;' style='color:#f58381;'><i class='iconfont' >&#xe65f;</i>删除</a></td></tr>")
                $("#mechanismModal").addClass("hide")
            }
            return false;
        }
    })
    //编辑投资机构
var mechanismtds = {}
$("body").on("click", ".editmechanism", function(e) {
    var e = event.target;
    if (e.tagName == "I") {
        mechanismtds = $(e).parent("a").parent("td").siblings();
    } else {
        mechanismtds = $(e).parent("td").siblings();
    }
    console.log(mechanismtds)
    var imgurl = $(mechanismtds[0]).find("img").attr("src")
    var editmechanismname = $(mechanismtds[1]).text()
    $(".editmechanismName").val(editmechanismname)
    $(".editmechanismurl>.list>img").attr("src", imgurl)

})
$(".editmechanismbtn").click(function() {
        var editmechanismForm = $('#editmechanism-form');
        editmechanismForm.validate();
        if (editmechanismForm.valid()) {
            var name = $(".editmechanismName").val()
            var url = $(".editmechanismurl>.list>img").attr("src")
            $(mechanismtds[1]).find("span").text(name)
            $(mechanismtds[1]).find("input").attr("value", name)
            $(mechanismtds[0]).find("img").attr("src", url)
            $(mechanismtds[0]).find("input").attr("value", url)
            $("#editmechanismModal").addClass("hide")

        }
        return false;

    })
    //添加众筹细则验证
var tokenModalForm = $('#tokenModal-form');
tokenModalForm.validate();
var detailName = "";
var detailInfo = "";
$(".detailName").blur(function() {
    detailName = $(".detailName").val();
})
$(".detailInfo").blur(function() {
    detailInfo = $(".detailInfo").val();
})

tokenModalForm.on("submit", function() {
        if (tokenModalForm.valid()) {
            console.log(detailName, detailInfo)
            var str = "<tr><td><input type='hidden' value='" + detailName + "' name='column_name[]'  /><span>" + detailName + "</span></td><td><input type='hidden' name='column_content[]' value='" + detailInfo + "' /><span>" + detailInfo + "</span><td class='mr'><a class='iconright editatetoken blue-color' href='javascript:;' data-target='#edittokenModal' data-toggle='modal'><i class='iconfont'>&#xe605;</i>编辑</a><a class='iconright blue-color delete' href='javascript:;' style='color:#f58381;'><i class='iconfont' >&#xe65f;</i>删除</a></td></tr>"
            $(".tokenlist").append(str)
            $("#tokenModal").addClass("hide")
        }
        return false;
    })
    //编辑众筹细则
var tokentds = {}
$("body").on("click", ".editatetoken", function(e) {
    var e = event.target;
    if (e.tagName == "I") {
        tokentds = $(e).parent("a").parent("td").siblings();
    } else {
        tokentds = $(e).parent("td").siblings();
    }
    var tokenName = $(tokentds[0]).find('span').text()
    var tokenInfo = $(tokentds[1]).find('span').text()
    console.log(tokenName)
    if (tokenName == "代币名称" || tokenName == "代币类型" || tokenName == "接受代币" || tokenName == "兑换比列") {
        $(".edittokenName").attr("disabled", true)
    } else {

        $(".edittokenName").attr("disabled", false)
    }
    $(".edittokenName").val(tokenName)
    $(".edittokenInfo").val(tokenInfo)

})
$(".edittokenbtn").click(function() {
        var edittokenModalForm = $('#edittokenModal-form');
        edittokenModalForm.validate();
        if (edittokenModalForm.valid()) {
            var edittokenName = $(".edittokenName").val()
            var edittokenInfo = $(".edittokenInfo").val()
            $(tokentds[0]).find("span").text(edittokenName)
            $(tokentds[0]).find("input").attr("value", edittokenName)
            $(tokentds[1]).find("span").text(edittokenInfo)
            $(tokentds[1]).find("input").attr("value", edittokenInfo)
            $("#edittokenModal").addClass("hide")

        }
        return false;
    })
    // //添加渠道
    // var raiseForm=$("#raiseModal-form")
    // raiseForm.validate()
    // raiseForm.on("submit",function(){
    //    if(raiseForm.valid()){
    //     var raiseName=$(".raiseName").val()
    //     var raiseLink=$(".raiseLink").val()
    //    var str="<tr><td>"+raiseName+"</td><td>"+raiseLink+"<td class='mr'><a class='iconright editraise blue-color' href='javascript:;' data-target='#editraiseModal' data-toggle='modal'><i class='iconfont'>&#xe605;</i>编辑</a><a class='iconright blue-color delete' href='javascript:;' style='color:#f58381;'><i class='iconfont' >&#xe65f;</i>删除</a></td></tr>"
    //    $(".raiselist").append(str)
    //    $("#raiseModal").addClass("hide")
    //      return false;
    //    }
    // })
    // //编辑渠道
    // var raisetds={}
    // $("body").on("click",".editraise",function(e){
    //   var  e=event.target;
    //  if(e.tagName=="I"){
    //   raisetds=$(e).parent("a").parent("td").siblings();
    //  }
    //  else{
    //   raisetds=$(e).parent("td").siblings();
    //  }
    //   var editraiseName=$(raisetds[0]).text()
    //   var editraiseLink=$(raisetds[1]).text()
    //   $(".editraiseName").val(editraiseName)
    //   $(".editraiseLink").val(editraiseLink)

// })
// $(".editraisebtn").click(function(){
//     var eeditraiseModalForm = $('#editraiseModal-form');
//     eeditraiseModalForm.validate();
//     if(eeditraiseModalForm.valid()){
//           var editraiseName=$(".editraiseName").val()
//           var editraiseLink=$(".editraiseLink").val()
//           $(raisetds[0]).text(editraiseName)
//           $(raisetds[1]).text(editraiseLink)
//           $("#editraiseModal").addClass("hide")    

//     }
//     return false;  
// })

//删除
$("body").on("click", '.delete', function() {
        var str = '<i class="iconfont" style="color:#eac149;margin-right:5px;">&#xe61a;</i>确认要删除添加的内容，是否继续?';
        var e = event.target;
        var tr = $(e).parent().parent();
        $.confirm({
            msg: str,
            confirm: function() {
                tr.remove()
            }
        })
    })
    //添加等级/评分验证
var rateForm = $("#rate-form")
rateForm.validate()
rateForm.on("submit", function() {
        if (rateForm.valid()) {

            if ($("textarea[name='rate_content']").length == 0 || $("textarea[name='rate_content']").val() == '') {
                $.alert('请填写内容!');
                return false;
            } else if ($('.rateurl>.list>img').length <= 0) {
                $.alert('请上传logo!');
                return false;
            } else {
                var rateName = $(".rateName").val()
                var rateMark = $(".rateMark").val()
                var rateContent = $("textarea[name='rate_content']").val()
                var url = $(".rateurl>.list>img").attr("src");
                $(".ratelist").append("<tr><td><input  value='" + url + "' name='organize_logo[]' type='hidden'/>  <img src='" + url + "' alt='' style='width:100px;height:100px' /></td><td><span>" + rateName + "</span><input  value='" + rateName + "' name='organize_name[]' type='hidden'/></td><td><input  value='" + rateMark + "' name='degree[]' type='hidden'/><span>" + rateMark + "</span></td><td class='mr'><input  value='" + rateContent + "' class='ueinfo'  name='rate_content[]' type='hidden'/><a class='iconright editrate blue-color' href='javascript:;' data-target='#editrateModal' data-toggle='modal' ><i class='iconfont'>&#xe605;</i>编辑</a><a class='iconright blue-color delete' href='javascript:;' style='color:#f58381;'><i class='iconfont' >&#xe65f;</i>删除</a></td></tr>")
                $("#rateModal").addClass("hide")
                return false;
            }

        }
    })
    //编辑等级
var ratetds = {}
$("body").on("click", ".editrate", function(e) {
    var e = event.target;
    if (e.tagName == "I") {
        ratetds = $(e).parent("a").parent("td").siblings();
    } else {
        ratetds = $(e).parent("td").siblings();
    }
    var imgurl = $(ratetds[0]).find("img").attr("src")
    var rateName = $(ratetds[1]).text()
    var rateMark = $(ratetds[2]).text()
    var rateContent = $(".ueinfo").attr("value")
    console.log(rateContent, 11111)
    $(".editrateName").val(rateName)
    $(".editrateMark").val(rateMark)
    var ue2 = UE.getEditor('goods_desc2', {
        //readonly: true,
        autoFloatEnabled: false,
        initialFrameWidth: 400,
        initialFrameHeight: 100,
    })
    $(document).ready(function() {
        var ue2 = UE.getEditor('goods_desc2');

        ue2.ready(function() { //编辑器初始化完成再赋值
            ue2.setContent(rateContent); //赋值给UEditor
        });
    })

    $(".editrateurl>.list>img").attr("src", imgurl)
})
$(".rateconfirm").click(function() {
        var editrateForm = $('#editrate-form');
        editrateForm.validate();
        if (editrateForm.valid()) {
            var rateName = $(".editrateName").val()
            var rateMark = $(".editrateMark").val()
            var rateContent = $("textarea[name='editrate_content']").val()
            var url = $(".editrateurl>.list>img").attr("src")
            $(ratetds[1]).find("span").text(rateName)
            $(ratetds[1]).find("input").attr("value", rateName)
            $(ratetds[2]).find("span").text(rateMark)
            $(ratetds[2]).find("input").attr("value", rateMark)
            $(ratetds[0]).find("img").attr("src", url)
            $(ratetds[0]).find("input").attr("value", url)
            $(".ueinfo").attr("value", rateContent)
            $("#editrateModal").addClass("hide")

        }
        return false;
    })
    //添加代币分配
var shareModalForm = $('#shareModal-form');
shareModalForm.validate()
shareModalForm.on("submit", function() {
    if (shareModalForm.valid()) {
        var detailName = $(".shareName").val()
        var detailInfo = $(".shareInfo").val()
        var str = "<tr><td><input type='hidden' value='" + detailName + "' name='param_name[]' /><span>" + detailName + "</span></td><td><input type='hidden' name='percent[]' value='" + detailInfo + "'/><span>" + detailInfo + "</span><td class='mr'><a class='iconright editshare blue-color' href='javascript:;' data-target='#editshareModal' data-toggle='modal'><i class='iconfont'>&#xe605;</i>编辑</a><a class='iconright blue-color delete' href='javascript:;' style='color:#f58381;'><i class='iconfont' >&#xe65f;</i>删除</a></td></tr>"
        $(".sharelist").append(str)
        $("#shareModal").addClass("hide")
    }
    return false;
})

//编辑代币
var sharetds = {}
$("body").on("click", ".editshare", function(e) {
    var e = event.target;
    if (e.tagName == "I") {
        sharetds = $(e).parent("a").parent("td").siblings();
    } else {
        sharetds = $(e).parent("td").siblings();
    }
    var shareName = $(sharetds[0]).text()
    var shareInfo = $(sharetds[1]).text()
    $(".editshareName").val(shareName)
    $(".editshareInfo").val(shareInfo)

})
$(".shareconfirm").click(function() {
    var editshareModalForm = $('#editshareModal-form');
    editshareModalForm.validate();
    if (editshareModalForm.valid()) {
        var editshareName = $(".editshareName").val()
        var editshareInfo = $(".editshareInfo").val()
        $(sharetds[0]).find("span").text(editshareName)
        $(sharetds[0]).find("input").attr("value", editshareName)
        $(sharetds[1]).find("span").text(editshareInfo)
        $(sharetds[1]).find("input").attr("value", editshareInfo)
        $("#editshareModal").addClass("hide")

    }
    return false;
})

//整体验证
var projectForm = $("#project-table")
projectForm.validate()
projectForm.on("submit", function() {
    if (projectForm.valid()) {
        if ($('.projectlogo>.list>img').length <= 0) {
            $.alert('请上传logo!');
            return false;
        }
    }
})